package com.fr.report.web.ui;

import com.fr.base.ArrayUtils;
import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.report.cellElement.Formula;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.script.Calculator;
import com.fr.report.script.ParameterMapNameSpace;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class IframeEditor extends FieldEditor
{
  public static final String XML_TAG = "Iframe";
  private String src;
  private Parameter[] parameters;

  public IframeEditor()
  {
  }

  public IframeEditor(String paramString)
  {
    this.src = paramString;
  }

  public String getSrc()
  {
    return this.src;
  }

  public void setSrc(String paramString)
  {
    this.src = paramString;
  }

  public Parameter[] getParameters()
  {
    return ((this.parameters == null) ? new Parameter[0] : this.parameters);
  }

  public void setParameters(Parameter[] paramArrayOfParameter)
  {
    this.parameters = paramArrayOfParameter;
  }

  public String getXType()
  {
    return "iframe";
  }

  public String[] supportedEvents()
  {
    return ArrayUtils.EMPTY_STRING_ARRAY;
  }

  public JSONArray createJSONData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    JSONArray localJSONArray = super.createJSONData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest);
    paramCalculator.pushNameSpace(ParameterMapNameSpace.create(paramSessionIDInfor.getParameterMap4Execute()));
    for (int i = 0; i < ((this.parameters == null) ? 0 : this.parameters.length); ++i)
    {
      Object localObject1 = this.parameters[i].getValue();
      if (localObject1 instanceof Formula)
      {
        localObject2 = ((Formula)localObject1).getContent();
        localObject1 = paramCalculator.eval((String)localObject2);
      }
      Object localObject2 = new JSONObject().put(this.parameters[i].getName(), localObject1);
      localJSONArray.put(localObject2);
    }
    return ((JSONArray)localJSONArray);
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = super.createJSONConfig(paramObject, paramRepository);
    if (StringUtils.isNotBlank(this.src))
      try
      {
        localJSONObject.put("src", TemplateUtils.render(this.src, paramRepository.checkoutTplContext()));
      }
      catch (Exception localException)
      {
        FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
      }
    return localJSONObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
      if (paramXMLableReader.getTagName().equals("Iframe"))
      {
        this.src = paramXMLableReader.getAttr("src");
      }
      else if ("Parameters".equals(paramXMLableReader.getTagName()))
      {
        Parameter[] arrayOfParameter = ReportXMLUtils.readParameters(paramXMLableReader);
        setParameters(arrayOfParameter);
      }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (StringUtils.isNotBlank(this.src))
      paramXMLPrintWriter.startTAG("Iframe").attr("src", this.src).end();
    ReportXMLUtils.writeParameters(paramXMLPrintWriter, getParameters());
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof IframeEditor) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.src, ((IframeEditor)paramObject).src)) && (ComparatorUtils.equals(this.parameters, ((IframeEditor)paramObject).parameters)));
  }
}